<template>
    <div class="login-wrap">
        <div class="ms-title">Live⁺云摄影</div>
        <div class="ms-login">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="password" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm('ruleForm')" v-text="$t('login.login')"></el-button>
                </div>
                <p style="font-size:12px;line-height:30px;color:#999;">Tips : {{$t('login.login_tip')}}</p>
            </el-form>
        </div>
    </div>
</template>

<script>
    import * as types from '../../store/types.js'
    import * as util from '../../assets/util.js';
    import * as params from '../../store/appServerParams.js'
    export default {
        data: function() {
            return {
                ruleForm: {
                    username: '',
                    password: ''
                    // username: '',
                    // password: ''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
                }
            }
        },
        methods: {
            //转换权限格式
            transOpersform(list){
                var result= [];
                var idstr = [];
                var pidstr = [];
                for (var i = 0; i < list.length; i++) {
                    list[i]=JSON.parse(list[i]);
                    idstr.push(list[i].operId)
                    pidstr.push(list[i].sysPoId)
                    list[i].url = list[i].url;
                    list[i].title = list[i].operName;
                    list[i].index = list[i].operName;
                }
                var temp = {}
                for (var i = 0; i < list.length; i++) {
                    temp[idstr[i]] = list[i] //将nodes数组转成对象类型
                }
                for (var j = 0; j < list.length; j++) {
                    var tempVp = temp[pidstr[j]] //获取每一个子对象的父对象
                    if (tempVp) {
                        //判断父对象是否存在，如果不存在直接将对象放到第一层
                        if (!tempVp['subs']) tempVp['subs'] = [] //如果父元素的nodes对象不存在，则创建数组
                        tempVp['subs'].push(list[j]) //将本对象压入父对象的nodes数组
                    } else {
                        result.push(list[j]) //将不存在父对象的对象直接放入一级目录
                    }
                }
                // console.log(list);
                return JSON.stringify(result);
            },
            // 请求登录接口数据
            requestLoginInterface() {
                var self = this
                self.$http.post('/login', {
                    username: self.ruleForm.username,
                    password: self.ruleForm.password
                })
                    .then(function(response) {
                        if (response.data[params.KEY_RESULT] == false) {
                            alert(response.data[params.ERR_KEY_MSG])
                            return
                        } else {

                            // self.$store.commit(types.USERNAME, self.ruleForm.username);

                            // self.$store.commit(types.LOGIN, tokenInfo);
                            // var auth = self.transOpersform(response.data.auth)
                            // self.$store.commit(types.AUTH, response.data.auth)//菜单列表
                            // var permissionList = JSON.stringify(response.data.auth);
                            // self.$store.commit(types.PERMISSION, permissionList)//菜单列表
                            //   self.$router.push('/')
                            util.local(params.TOKEN, response.data.token);
                            util.local(params.AUTH_ROUTER,response.data.auth);
                            util.local(params.AUTH_RESOURCE,response.data.user.authUrl);
                            let tokenInfo = {
                                token:response.data.token,
                                createTime:response.data.createTime,
                                expireTime:response.data.expireTime,
                                refreshTime:response.data.refreshTime,
                            }
                            util.local(params.TOKENINFO,tokenInfo);
                            util.local(params.USERNAME,response.data.user.username);
                            self.$emit('login', self.$router.currentRoute.query.from);
                        }
                    })
                    .catch(function(error) {
                        console.log(error)
                    })
            },

            submitForm(formName) {
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        this.requestLoginInterface();



                    } else {
                        console.log('error submit!!')
                        return false
                    }
                })
            }
        },
        created: function () {
            // let vm = this
            // if(util.local('token') != undefined && util.local('token') != ''){
            //     vm.$router.replace({path: '/'});
            // }
        }
    }
</script>

<style scoped>
    .login-wrap {
        position: relative;
        width: 100%;
        height: 100%;
    }
    .ms-title {
        position: absolute;
        top: 50%;
        width: 100%;
        margin-top: -230px;
        text-align: center;
        font-size: 30px;
        color: #fff;
    }
    .ms-login {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 300px;
        height: 160px;
        margin: -150px 0 0 -190px;
        padding: 40px;
        border-radius: 5px;
        background: #fff;
    }
    .login-btn {
        text-align: center;
    }
    .login-btn button {
        width: 100%;
        height: 36px;
    }
</style>
